package com.sunfeng.netty.atguigu.codec3;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sunfeng
 * @date 2021/05/19 15:11
 **/
@Slf4j
public class SubReqClientHandler extends SimpleChannelInboundHandler<SubscribeResp> {

    public SubReqClientHandler() {
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SubscribeResp msg) throws Exception {

        log.info("接受到的服务端的结果为：【" + msg.toString() + "】");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i++) {
            ctx.writeAndFlush(subReq(i));
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error(cause.getMessage());
    }

    private SubScribeReq subReq(int i) {
        SubScribeReq subScribeReq = new SubScribeReq();
        subScribeReq.setAddress("山东菏泽曹县NP,666");
        subScribeReq.setPhoneNumber("18354680214");
        subScribeReq.setProductName("Netty 权威指南");
        subScribeReq.setSubReqID(i);
        subScribeReq.setUserName("SunFeng");
        return subScribeReq;

    }
}
